package com.itheima.io.utils;

import java.io.*;

public class IOUtils {

    public static void copyFileWithCharStream(Reader reader, Writer writer) {

        try {

            char[] chs = new char[1024];
            int len = -1;
            while ((len = reader.read(chs)) != -1) {
                writer.write(chs, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

    public static void copyFileWithByteStream(InputStream in, OutputStream out) {
        try {

            byte[] bytes = new byte[1024];
            int len = -1;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}
